package com.example.controller;

import com.example.entity.PageBean;
import com.example.entity.Result;
import com.example.entity.Schedule;
import com.example.entity.Task;
import com.example.entity.vo.Progress;
import com.example.entity.vo.ScheduleTask;
import com.example.entity.vo.TaskResponse;
import com.example.service.ScheduleService;
import com.example.utils.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/schedule")
@Slf4j
public class ScheduleController {

    @Autowired
    private ScheduleService scheduleService;

    @GetMapping
    public Result<PageBean> getScheduleById(@RequestParam(defaultValue = "1") Integer page,
                                        @RequestParam(defaultValue = "10") Integer pageSize,
                                        String title) {
        log.info("课程计划分页查询：{},{},{}", page, pageSize, title);
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        PageBean pageBean = scheduleService.page(page,pageSize,title,id);
        return Result.success(pageBean);
    }

    @PostMapping
    public Result<Integer> addSchedule(@RequestBody Map<String, Object> request){
        String title = (String) request.get("title");
        log.info("计划名称：{}",title);
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        Integer id = scheduleService.addSchedule(title,userId);
        if (id < 0){
            return Result.error("该计划已存在");
        }
        return Result.success(id);
    }

    //学习进度
    @GetMapping("/progress")
    public Result<List<Schedule>> getProgress() {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");

        List<Schedule> schedules = scheduleService.getSchedulesByUserId(userId);
        return Result.success(schedules);
    }

    //学习任务
    @GetMapping("/tasks")
    public Result<List<Task>> getTasksByScheduleId(@RequestParam Integer scheduleId) {
        List<Task> tasks = scheduleService.getTasksByScheduleId(scheduleId);
        return Result.success(tasks);
    }

    @GetMapping("/check")
    public Result<Boolean> checkSchedule(@RequestParam String title){
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        Boolean bool = scheduleService.checkSchedule(userId,title);
        return Result.success(bool);
    }

    @DeleteMapping
    public Result<Object> deleteSchedule(@RequestBody Map<String, Object> request){
        String title = (String) request.get("title");
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        scheduleService.deleteSchedule(id,title);
        return Result.success();
    }

    @PostMapping("/task")
    public Result<Object> addTasks(@RequestBody TaskResponse taskResponse){
        scheduleService.addTasks(taskResponse);
        return Result.success();
    }

    @GetMapping("/totalProgress")
    public Result<Progress> getTotalSchedule(){
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        Progress progress = scheduleService.getTotalSchedule(id);
        return Result.success(progress);
    }

    @GetMapping("/display")
    public Result<List<ScheduleTask>> scheduleDisplay(){
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        List<ScheduleTask> scheduleDisplays = scheduleService.scheduleDisplay(id);
        return Result.success(scheduleDisplays);
    }

    @PatchMapping("/finish")
    public Result<Object> updateFinish(@RequestBody Map<String, Object> request){
        Integer taskId = Integer.parseInt(request.get("taskId").toString());
        scheduleService.updateFinish(taskId);
        return Result.success();
    }
}
